import http from '../../../http/http'
import * as url from '../../../http/api'
const state = {
    allDishType:[],//所有类别
    dishData:[],//菜品数据
    currentDishType:[],//分页类别
    TypePageSize:0,//分页类别总页数
    pageSize:0,//菜品数据总页数
}
const actions = {
    getTypeData({commit},obj){
        http({
            url:url.foodTypeManagementSelect,
            method:'post',
            data:obj
        }).then(res=>{
            if(res.data.status == 'success'){
                console.log(res.data,656464)
                commit('saveCurrentType',res.data)
            }    
        }).catch(error=>{
            console.log(error)
        })
    },
    searchDishType({commit},obj){
        http({
            url:url.foodTypeManagementSelect,
            method:'post',
            data:obj,
        }).then(res=>{
            if(res.data.status == 'success'){
                console.log(res.data,'搜索')
                commit('saveDish',res.data)
            }    
        }).catch(error=>{
            console.log(error)
        })
    },
    addDishType({commit},obj){     
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodTypeManagementAdd,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res,'添加类别')
                if(res.data.status == 'success'){
                    resolve('success')
                }else{
                    resolve('error')
                }  
            })
        })
    },
    editDishType({commit},obj){   
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodTypeManagementUpdate,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res,'修改')
                if(res.data.status == 'success'){
                    resolve('success')
                }else{
                    resolve('error')
                }   
            })
        })
    },
    deleteDish({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementDelete,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res.data)
                if(res.data.status == 'success'){
                   resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })
        
    },
    getUnderTypeDish({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodManagementUnderType,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res,'获取类别下菜品')
                if(res.data.status == 'success'){
                    commit('savaUnderTypeDish',res.data)
                    resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })
       
    },
    getAllType({commit}){
        http({
            url:url.foodManagementAllDishType,
            method:'post',
        }).then(res=>{
            if(res.data.status == 'success'){
                console.log(res,'获取所有类别')
                commit('saveAllDishType',res.data.getDishesClassifyOfNameList)
            }    
        }).catch(error=>{
            console.log(error)
        })
    },
    deleteType({commit},obj){
        return new Promise((resolve,reject)=>{
            http({
                url:url.foodTypeManagementDelete,
                method:'post',
                data:obj,
            }).then(res=>{
                console.log(res,'删除')
                if(res.data.status == 'success'){
                    resolve('success')
                }else{
                    resolve('error')
                }    
            })
        })
        
    }
    
}
const mutations = {
    saveDish(state,data){
        state.dishData = data.listDishes;
        state.pageSize = data.totalPage;

    },
    savaUnderTypeDish(state,data){
        state.dishData = data.listDishesMain;
        state.pageSize = data.totalPage;
    },
    saveCurrentType(state,data){
        state.currentDishType = data.listDishesClassify;
        state.TypePageSize = data.totalPage;
    },
    saveAllDishType(state,data){
        state.allDishType = data
    }
}
const getters = {

}
export default{
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}